package com.micosco.taskremind.ui;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.micosco.taskremind.R;
import com.micosco.taskremind.data.Task;

import java.util.ArrayList;
import java.util.List;

/**
 * {@link RecyclerView.Adapter} that can display a {@link Task}.
 * TODO: Replace the implementation with code for your data type.
 */
public class TaskListRecyclerViewAdapter extends RecyclerView.Adapter<TaskViewHolder> {
    private final List<Task> mValues;
    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public TaskListRecyclerViewAdapter(List<Task> items) {
        if (items == null) {
            mValues = new ArrayList<>();
        } else {
            mValues = items;
        }
    }

    @NonNull
    @Override
    public TaskViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_task_entry, parent, false);

        return new TaskViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final TaskViewHolder holder, int position) {
        holder.mItem = mValues.get(position);
        holder.mSubjectView.setText(mValues.get(position).getSubject());
        holder.mTitleView.setText(mValues.get(position).getTitle());
        holder.mRemainingTimeView.setText(mValues.get(position).remainingTimeString());

        if (mOnItemClickListener != null) {
            holder.layout.setOnClickListener(v -> mOnItemClickListener.onItemClick(v, position));
        }
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
}